﻿using Ledict.ConditionExtract.Base;
using Ledict.ConditionExtract.BLL;
using Ledict.ConditionExtract.Common;
using Ledict.ConditionExtract.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Ledict.ConditionExtract.Frm.NodeTree
{
    public partial class HanShuConfig : BaseForm
    {
        public ConvertMode cm { get; set; }
        public ConvertMode cmUpdate { get; set; }
        ConvertModeBLL convertModeBll = new ConvertModeBLL();
        ExtractFeatureBLL efBll = new ExtractFeatureBLL();
        public bool IsAdd { get; set; }
        public string state = string.Empty;
        public string efId = string.Empty;
        public HanShuConfig()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 取消按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCanel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.No;
        }

        /// <summary>
        /// 保存按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(cbtHanShu.Text))
            {
                MessageBox.Show("函数值不能为空！");
                cbtHanShu.Focus();
                return;
            }
            if (cmbGS.Visible)
            {
                if (string.IsNullOrEmpty(cmbGS.Text.Trim()))
                {
                    MessageBox.Show("请进行格式选择！");
                    cmbGS.Focus();
                    return;
                }
            }
            if (cm == null)
            {
                cm = new ConvertMode();
            }
            cm.CM_UpdateTime = DateTime.Now;
            if (cbtHanShu.TreeView.SelectedNode == null)
            {
                if (ElementsConfig.dicYaoSu.Count > 0)
                {
                    foreach (var item in ElementsConfig.dicYaoSu.Keys)
                    {
                        if (ElementsConfig.dicYaoSu[item].Split(',')[0].ToString() == cbtHanShu.Text)
                        {
                            cm.CM_Method = item;
                            cm.CM_CMode = Convert.ToInt32(ElementsConfig.dicYaoSu[item].Split(',')[1].ToString());
                            break;
                        }
                    }
                }
            }
            else
            {
                cm.CM_Method = cbtHanShu.TreeView.SelectedNode.Tag.ToString().Split(',')[0].ToString();
                cm.CM_CodeCH = cbtHanShu.TreeView.SelectedNode.Text.ToString();
            }
            if (cmbGS.Visible)
            {
                cm.CM_MethodParameter = cmbGS.SelectedValue.ToString();
            }
            else
            {
                cm.CM_MethodParameter = "";
            }
            if (cbtHanShu.TreeView.SelectedNode != null)
            {
                cm.CM_CMode = Convert.ToInt32(cbtHanShu.TreeView.SelectedNode.Tag.ToString().Split(',')[1].ToString());
            }
               
            cm.CM_Code = cm.CM_Method;
            if (state == "修改" && cmUpdate !=null&&(cmUpdate.CM_MethodValue != cm.CM_Method || cmUpdate.CM_MethodParameterValue != cm.CM_MethodParameter) && cm.CM_Custom == 0)
            {
                cm.CM_ID = Snowflake.GetId().ToString();
                cm.CM_Custom = 1;
                convertModeBll.ConvertModeAddHanShu(cm);
                MessageBox.Show("修改成功！");
            }
            else  if (!IsAdd && !string.IsNullOrEmpty(cm.CM_ID) && state == "修改")
            {
                if (convertModeBll.ConvertModeHanShuUpdate(cm))
                {
                    MessageBox.Show("修改成功！");
                }
                else
                {
                    MessageBox.Show("修改失败！");
                }

            }
            else if (!IsAdd && !string.IsNullOrEmpty(cm.CM_ID) && state == "新增")
            {
                cm.CM_Custom = 1;
                if (convertModeBll.ConvertModeAddHanShu(cm))
                {
                    MessageBox.Show("添加成功！");
                }
                else
                {
                    MessageBox.Show("添加失败！");
                }
            }
            else if (!IsAdd && string.IsNullOrEmpty(cm.CM_ID) && state == "新增" && !string.IsNullOrEmpty(efId))
            {
                cm.CM_Custom = 1;

                cm.CM_ID = Snowflake.GetId().ToString();
                ExtractFeature ef = new ExtractFeature() { EF_ID = efId, CM_ID = cm.CM_ID };
                if (convertModeBll.ConvertModeAddHanShu(cm) && efBll.ExtractFeatureUpdateCMID(ef))
                {
                    MessageBox.Show("添加成功！");
                }
                else
                {
                    MessageBox.Show("添加失败！");
                }
            }
            else if (string.IsNullOrEmpty(cm.CM_ID)&&IsAdd)
            {
                cm.CM_Custom = 1;
                cm.CM_ID = Snowflake.GetId().ToString();
                if (convertModeBll.ConvertModeAddHanShu(cm))
                {
                    MessageBox.Show("添加成功！");
                }
                else
                {
                    MessageBox.Show("添加失败！");
                }
            }
            DialogResult = DialogResult.Yes;
            this.Close();
        }

        /// <summary>
        /// 加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ArgumentConfig_Load(object sender, EventArgs e)
        {
            if (ElementsConfig.dicYaoSu.Count > 0)
            {
                foreach (var item in ElementsConfig.dicYaoSu.Keys)
                {
                    //key：convertdatetime value:时间标准化+"1"
                    TreeNode node = new TreeNode() { Text = ElementsConfig.dicYaoSu[item.Trim()].Split(',')[0].ToString(), Tag = item.Trim()+"," + ElementsConfig.dicYaoSu[item.Trim()].Split(',')[1].ToString() };
                    cbtHanShu.TreeView.Nodes.Add(node);
                }
            }

            //Dictionary<string, string> getData = new Dictionary<string, string>();
            //CommonMsgNew1 cm1 = WebUtil.DoGet<CommonMsgNew1>("http://192.168.1.73:6600/" + "api/WebApi/GetSpecialFunDataAnalysis", getData, "gb2312");
            //if (cm1.Code == "1")
            //{
            //    string s1 = cm1.CfgContent;
            //    res f1 = Newtonsoft.Json.JsonConvert.DeserializeObject<res>(s1);
            //    if (f1.fun.Count > 0)
            //    {
            //        foreach (fun item in f1.fun)
            //        {
            //            TreeNode node = new TreeNode() { Text = item.remark.Trim(), Tag =item.name.Trim() };
            //            cbtHanShu.TreeView.Nodes.Add(node);
            //        }
            //    }
            //}            
            cbtHanShu.DropDownStyle = ComboBoxStyle.DropDown;

            if (cm != null && !string.IsNullOrEmpty(cm.CM_ID))
            {
                cbtHanShu.Text = cm.CM_Method;
                if (!string.IsNullOrEmpty(cm.CM_MethodParameter))
                {
                    label1.Visible = cmbGS.Visible = panelGS.Visible = true;
                    cmbGS.DisplayMember = "remark";
                    cmbGS.ValueMember = "name";
                    if (ElementsConfig.dicYaoSu.Count > 0)
                    {
                        foreach (var item in ElementsConfig.dicYaoSu.Keys)
                        {
                            if (ElementsConfig.dicYaoSu[item].Contains(cm.CM_Method))
                            {
                                cmbGS.DataSource = ElementsConfig.dicYaoSuParams[item];
                                break;
                            }
                        }
                    }
                    cmbGS.Text = cm.CM_MethodParameter;
                }
                cmUpdate = new ConvertMode();
                cmUpdate.CM_ID = cm.CM_ID;
                cmUpdate.CM_MethodValue = cm.CM_MethodValue;
                cmUpdate.CM_MethodParameterValue = cm.CM_MethodParameterValue;
            }
        }




        private void cbtHanShu_TextChanged(object sender, EventArgs e)
        {
            if (cbtHanShu.TreeView.SelectedNode != null && !string.IsNullOrEmpty(cbtHanShu.TreeView.SelectedNode.Tag.ToString()) && ElementsConfig.dicYaoSuParams.ContainsKey(cbtHanShu.TreeView.SelectedNode.Tag.ToString().Split(',')[0].ToString()))
            {
                label1.Visible = cmbGS.Visible = panelGS.Visible = true;
                cmbGS.DisplayMember = "remark";
                cmbGS.ValueMember = "name";
                cmbGS.DataSource = ElementsConfig.dicYaoSuParams[cbtHanShu.TreeView.SelectedNode.Tag.ToString().Split(',')[0].ToString()];
            }
            else
            {
                cmbGS.DataSource = null;
                label1.Visible = cmbGS.Visible = panelGS.Visible = false;

            }
        }
    }
}
